/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						       dialog_campaign_selection.cpp

	$Header: /game/dialog_campaign_selection.cpp $

	$NoKeywords: $

 ************************************************************************/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include <algorithm>
#include <assert.h>
#include <utility>
#include <fstream>
#include <strstream>

#include "dialog_campaign_selection.h"

#include "adventure_frame.h"
#include "adventure_map.h"
#include "bitmap_group_cache.h"
#include "bitmap_layer_window.h"
#include "bitmap_layer_cache_window.h"
#include "bound_handler.h"
#include "button.h"
#include "button_cache.h"
#include "dialog_campaign.h"
#include "elements_of.h"
#include "enum_operations.h"
#include "external_string.h"
#include "format_string.h"
#include "interface_help.h"
#include "item_help_block.h"
#include "minimum_maximum.h"
#include "platform.h"
#include "replace_keywords.h"
#include "standard_campaign_id.h"
#include "standard_fonts.h"
#include "text_window.h"
#include "text_edit_window.h"


static t_bitmap_group_cache g_layout( "dialog.campaign_selection" );
static t_bitmap_group_cache g_chaos_intro( "campaign_splashscreens.192x154.chaos_intro" );
//static t_bitmap_group_cache g_chaos_intro_completed( "campaign_splashscreens.192x154.chaos_intro_completed" );
static t_bitmap_group_cache g_death_intro( "campaign_splashscreens.192x154.death_intro" );
//static t_bitmap_group_cache g_death_intro_completed( "campaign_splashscreens.192x154.death_intro_completed" );
static t_bitmap_group_cache g_life_intro( "campaign_splashscreens.192x154.life_intro" );
//static t_bitmap_group_cache g_life_intro_completed( "campaign_splashscreens.192x154.life_intro_completed" );
static t_bitmap_group_cache g_magic_intro( "campaign_splashscreens.192x154.magic_intro" );
//static t_bitmap_group_cache g_magic_intro_completed( "campaign_splashscreens.192x154.magic_intro_completed" );
static t_bitmap_group_cache g_might_intro( "campaign_splashscreens.192x154.might_intro" );
//static t_bitmap_group_cache g_might_intro_completed( "campaign_splashscreens.192x154.might_intro_completed" );
static t_bitmap_group_cache g_nature_intro( "campaign_splashscreens.192x154.nature_intro" );
//static t_bitmap_group_cache g_nature_intro_completed( "campaign_splashscreens.192x154.nature_intro_completed" );
static t_bitmap_group_cache g_order_intro( "campaign_splashscreens.192x154.order_intro" );
//static t_bitmap_group_cache g_order_intro_completed( "campaign_splashscreens.192x154.order_intro_completed" );
extern t_button_cache g_cancel_button;


namespace
{

	t_standard_campaign_id const k_campaign_ids[ t_campaign_selection_dialog::k_campaign_count ] =
	{
		k_standard_campaign_life,
		k_standard_campaign_order,
		k_standard_campaign_death,
		k_standard_campaign_chaos,
		k_standard_campaign_nature,
		k_standard_campaign_might,
	};

}

// ---------------------------------------------------------------------
// score dialog
// ---------------------------------------------------------------------
t_campaign_selection_dialog::t_campaign_selection_dialog( t_window* parent )
				: t_bitmap_layer_window( 0, t_screen_point(0,0) ),
					m_difficulty( k_difficulty_normal ),
					m_guards_can_move( true )
				  
{
	initialize();
}

// ---------------------------------------------------------------------
// add a text item
// ---------------------------------------------------------------------
t_text_window* t_campaign_selection_dialog::add_text( std::string const& text, std::string const& layer_name, 
							               int size, bool size_explicit, bool center )
{
	t_bitmap_layer const* layer;
	t_screen_rect         rect;
	t_text_window*        text_window;
	t_cached_font         font;

	layer = m_layout->find( layer_name );
	rect = layer->get_rect();
	if( size_explicit )
		font = get_font( size );
	else
		font = get_font( rect.height() / size );
	text_window = new t_text_window( font, rect, this, text, t_pixel_24(0,0,0));
	if( center )
		text_window->set_center_horizontal();
	text_window->set_drop_shadow( true, t_pixel_24(200,200,200));
	return text_window;
}

// -----------------------------------------------------------------------
// score dialog
// -----------------------------------------------------------------------
void t_campaign_selection_dialog::cancel_clicked( t_button* button )
{
	close();	// close all child windows too
}

// ---------------------------------------------------------------------
// create buttons
// ---------------------------------------------------------------------
void t_campaign_selection_dialog::create_buttons()
{
	int						i;
	t_bitmap_layer const*	layer;
	t_window*				window;
	t_screen_point			point(0,0);
	t_screen_rect			rect;
	t_button*				button;
	t_help_block const&		help = get_help_block( "shared" );
	t_help_block const&		campaign_help = get_help_block( "campaign_selection" );

	// create cancel button
	rect = m_layout->find( "cancel_button" )->get_rect();
	button = new t_button( g_cancel_button.get(), rect.top_left(), this );
	button->set_click_handler( bound_handler(*this, &t_campaign_selection_dialog::cancel_clicked ) );
	set_help( button, help, "cancel" );

	for( i = 0; i < k_campaign_count; ++i )
	{
		std::string const & keyword = get_keyword( k_campaign_ids[ i ] );
		std::string name = keyword;
		name += "_campaign";
		layer = m_layout->find( name );
		rect = layer->get_rect();
		button = new t_button( rect.top_left(), this );
		set_help( button, campaign_help, keyword );

		switch( k_campaign_ids[ i ] )
		{
		case k_standard_campaign_order:
			button->set_click_handler( bound_handler(*this, &t_campaign_selection_dialog::order_clicked ) );
//			if( m_enabled[k_standard_campaign_order] )
				layer = g_order_intro.get()->find( "frame 001" ); 
//			else
//				layer = g_order_intro_completed.get()->find( "frame 001" ); 
			break;
		case k_standard_campaign_might:
			button->set_click_handler( bound_handler(*this, &t_campaign_selection_dialog::might_clicked ) );
//			if( m_enabled[k_standard_campaign_might] )
				layer = g_might_intro.get()->find( "frame 001" ); 
//			else
//				layer = g_might_intro_completed.get()->find( "frame 001" ); 
			break;
		case k_standard_campaign_life:
			button->set_click_handler( bound_handler(*this, &t_campaign_selection_dialog::life_clicked ) );
//			if( m_enabled[k_standard_campaign_life] )
				layer = g_life_intro.get()->find( "frame 001" ); 
//			else
//				layer = g_life_intro_completed.get()->find( "frame 001" ); 
			break;
		case k_standard_campaign_death:
			button->set_click_handler( bound_handler(*this, &t_campaign_selection_dialog::death_clicked ) );
//			if( m_enabled[k_standard_campaign_death] )
				layer = g_death_intro.get()->find( "frame 001" ); 
//			else
//				layer = g_death_intro_completed.get()->find( "frame 001" ); 
			break;
		case k_standard_campaign_nature:
			button->set_click_handler( bound_handler(*this, &t_campaign_selection_dialog::nature_clicked ) );
//			if( m_enabled[k_standard_campaign_nature] )
				layer = g_nature_intro.get()->find( "frame 001" ); 
//			else
//				layer = g_nature_intro_completed.get()->find( "frame 001" ); 
			break;
		case k_standard_campaign_chaos:
			button->set_click_handler( bound_handler(*this, &t_campaign_selection_dialog::chaos_clicked ) );
//			if( m_enabled[k_standard_campaign_chaos] )
				layer = g_chaos_intro.get()->find( "frame 001" ); 
//			else
//				layer = g_chaos_intro_completed.get()->find( "frame 001" ); 
			break;
		}

		window = new t_bitmap_layer_window( layer, point, button );
		button->set_released_image( window );
	}
}

// ---------------------------------------------------------------------
// create icons
// ---------------------------------------------------------------------
void t_campaign_selection_dialog::create_icons()
{
	t_bitmap_layer const* layer;
	t_window*             window;
	t_screen_point        point(0,0);
	t_screen_rect         rect;

	// icons
	layer = m_layout->find( "frame" );
	window = new t_bitmap_layer_window( layer, point, this );
}

// ---------------------------------------------------------------------
// create all the text to go in each text box
// ---------------------------------------------------------------------
void t_campaign_selection_dialog::create_text()
{
}

// ---------------------------------------------------------------------
// score dialog
// ---------------------------------------------------------------------
static t_external_string const k_score_title( "title.campaign_selection" );
void t_campaign_selection_dialog::initialize()
{
	m_layout = g_layout.get();
//	std::fill( m_enabled, m_enabled + ELEMENTS_OF( m_enabled ), true );
	m_campaign_id = k_standard_campaign_none;

	//////////////////////////////////////////////////////////////////
 	// find dimensions & center
	t_screen_point        origin(0,0);
	t_bitmap_layer const* layer = m_layout->find( "background" );
	t_screen_rect		  rect = layer->get_rect();

	set_bitmap( layer );
	rect += t_screen_point( (get_parent()->get_width() - rect.width()) / 2, 
		                           (get_parent()->get_height() - rect.height()) / 2 );
	move( rect );
	set_drop_shadow();

	//////////////////////////////////////////////////////////////////

	// add title
	t_text_window* window;
	layer = m_layout->find( "title" );
	rect = layer->get_rect();
	window = new t_text_window( get_font( rect.height() ), rect, this, 
		                         k_score_title, t_pixel_24(0,0,0) );
	window->set_center_horizontal();
	window->set_center_vertical();
	window->set_drop_shadow( true, t_pixel_24(200,200,200));

	create_text();
	create_buttons();
	create_icons();
}

// ---------------------------------------------------------------------
// run the campaign dialog
// ---------------------------------------------------------------------
bool t_campaign_selection_dialog::do_campaign_dialog( int campaign_id )
{
	int selection;
	for ( selection = 0; true; ++selection )
	{
		assert( selection < k_campaign_count );
		if ( campaign_id == k_campaign_ids[ selection ] )
			break;
	}

//	if( m_enabled[selection] )
	{
		m_campaign_id = t_standard_campaign_id( campaign_id );

		// Load this game file.
		// This MUST work, so no error checking
		m_file_header_ptr = create_multi_scenario_campaign_file_header( m_campaign_id, 0 );
		assert( m_file_header_ptr.get() != 0 );

		// campaign info dialog
		t_counted_ptr<t_multi_scenario_dialog> campaign_dialog;
		campaign_dialog = new t_multi_scenario_dialog( t_window::get_main_window(), m_file_header_ptr, 0 );
		set_visible( false );
		if( campaign_dialog->run_modal() )
		{
			m_difficulty = campaign_dialog->get_difficulty();
			m_guards_can_move = campaign_dialog->guards_can_move();
			m_return_value = true;
			close();

			return true;
		}
	}

	set_visible( true );

	return false;
}

// ---------------------------------------------------------------------
// order campaign selection handler
// ---------------------------------------------------------------------
void t_campaign_selection_dialog::order_clicked( t_button* button )
{
	do_campaign_dialog( k_standard_campaign_order );
}

// ---------------------------------------------------------------------
// might campaign selection handler
// ---------------------------------------------------------------------
void t_campaign_selection_dialog::might_clicked( t_button* button )
{
	do_campaign_dialog( k_standard_campaign_might );
}

// ---------------------------------------------------------------------
// life campaign selection handler
// ---------------------------------------------------------------------
void t_campaign_selection_dialog::life_clicked( t_button* button )
{
	do_campaign_dialog( k_standard_campaign_life );
}

// ---------------------------------------------------------------------
// death campaign selection handler
// ---------------------------------------------------------------------
void t_campaign_selection_dialog::death_clicked( t_button* button )
{
	do_campaign_dialog( k_standard_campaign_death );
}

// ---------------------------------------------------------------------
// nature campaign selection handler
// ---------------------------------------------------------------------
void t_campaign_selection_dialog::nature_clicked( t_button* button )
{
	do_campaign_dialog( k_standard_campaign_nature );
}

// ---------------------------------------------------------------------
// chaos campaign selection handler
// ---------------------------------------------------------------------
void t_campaign_selection_dialog::chaos_clicked( t_button* button )
{
	do_campaign_dialog( k_standard_campaign_chaos );
}
